<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use app\common\model\MemberModel;
use app\common\model\GoodsOrderModel;
use app\common\model\RechargeModel;
class Index  extends Base
{
    /**
     * 欢迎页
     */
    public function index()
    {
        $this->needLogin();
        //代理总数量
        $memberCount= MemberModel::instance()->whereIn('status',[1,2])->count();
        //品正商数量
        $gradeCount= MemberModel::instance()->where('status',1)->where('grade',1)->count();
        //品正商V数量
        $gradeVCount = MemberModel::instance()->where('status',1)->where('grade',2)->count();
        //待审核数量
        $auditCount = MemberModel::instance()->where('status',2)->count();
        //今日注册量
        $todayRegCount = MemberModel::instance()->whereIn('status',[1,2])->where('addtime','between',[date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')])->count();

        $map = GoodsOrderModel::instance()->group('ispay')->column('count(0) as count','ispay');
        
        $todayOrderCount = GoodsOrderModel::instance()->where('addtime','between',[date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')])->count();
        
        
        $order = [
            'total'=>0,
            'cancel_count'=>0,
            'audit_count'=>0,
            'send_count'=>0,//发货
            'finish_count'=>0,//发货
            'today_order_count'=>$todayOrderCount,
        ];
        $total = 0;
        foreach ($map as $id=>$val){
            switch ($id){
                case 1:
                    $order['audit_count'] = $val;
                    break;
                case 2:
                    $order['cancel_count'] = $val;
                    break;
                case 3:
                    $order['send_count'] = $val;
                    break;
                case 5:
                    $order['finish_count'] = $val;
                    break;
            }
            $total += $val;
        }
        $order['total'] = $total;
        
        $data = [
            'memberCount'=>$memberCount,
            'gradeCount'=>$gradeCount,
            'gradeVCount'=>$gradeVCount,
            'auditCount'=>$auditCount,
            'todayRegCount'=>$todayRegCount,
        ];
        
        //订单金额
        $totalOrderMoney = GoodsOrderModel::instance()->whereIn('ispay',[1,3,5])->sum('total');
        //锁定金额
        $lockOrderMoney = GoodsOrderModel::instance()->whereIn('ispay',[1])->sum('total');
        
        //购买用户数
        $orderUserCount = GoodsOrderModel::instance()->whereIn('ispay',[1,3,5])->count('distinct mid');
        
        $order['total_order_money'] = $totalOrderMoney;
        $order['lock_order_money'] = $lockOrderMoney;
        $order['orderUserCount'] = $orderUserCount;
        
        //充值数
        $rechargeMap = RechargeModel::instance()->where('type',1)->where('ifpass',1)->group('recharge_type')->column('recharge_type,count(0) count,sum(money) money');
        $rechargeAuditCount = RechargeModel::instance()->where('type',1)->where('ifpass',0)->count();
        $rechargeFrontCount = 0;//前台充值个数
        $rechargeFrontMoney = 0;//前台充值金额
        if($rechargeMap){
            if(isset($rechargeMap[1])){
                $rechargeFrontCount =  $rechargeMap[1]['count'];
                $rechargeFrontMoney =  $rechargeMap[1]['money'];
            }
            if(isset($rechargeMap[2])){
                $rechargeBackCount =  $rechargeMap[2]['count'];
                $rechargeBackMoney =  $rechargeMap[2]['money'];
            }
        }
        
        $rechargeCount = $rechargeBackCount + $rechargeFrontCount; 
        $rechargeMoney = $rechargeBackMoney + $rechargeFrontMoney;
        
        $recharge = ['rechargeCount'=>$rechargeCount, 
            'rechargeMoney'=>$rechargeMoney,        
            'rechargeFrontCount'=>$rechargeFrontCount,        
            'rechargeFrontMoney'=>$rechargeFrontMoney,        
            'rechargeBackCount'=>$rechargeBackCount,       
            'rechargeBackMoney'=>$rechargeBackMoney,        
            'rechargeAuditCount'=>$rechargeAuditCount
            
        ];        
        
        $this->assign($data);
        $this->assign($recharge);
        $this->assign('order',$order);
        $this->assign('sys_info', $this->get_sys_info());
        return $this->fetch();
    }

    /**
     * 管理员登陆
     */
    public function login()
    {
        if (session('admin_id') && session('admin_id') > 0) {
            $this->success("您已登录", 'Index/index');
        }

        if($this->request->isPost()){
            $data = input('post.');
            $data['password']= md5($data['password']);
            /*$db = Db::name('admin')->where($data)->find();
            if ($db) {
                //管理员ID
                session('admin_id', $db['id']);
                //管理员昵称
                session('nickname', $db['nickname']);
                //最后登陆时间
                session('last_login_time', $db['login_time']);
                //最后登陆IP
                session('last_login_ip', $db['login_ip']);
                // 更新登陆时间与登陆IP
                Db::name('admin')->where('id' , $db['id'])->update(['login_time' => time(),'login_ip'=>request()->ip()]);

                return $this->success('登陆成功', 'Index/index');
            } else {
                return $this->error('账号密码不正确',  'Index/login');
            }*/
            return $this->success('登陆成功', 'Index/index');
        }

        return $this->fetch();
    }

    /**
     * 退出登陆
     */
    public function logout()
    {
        session_unset();
        session::clear();
        $this->success("退出成功", 'Index/login');
    }

    /**
     * 系统信息
     */
    public function get_sys_info()
    {
        $this->needLogin();
        $sys_info = [];
        $sys_info['os']             = PHP_OS;
        $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO'; //zlib
        $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO'; //safe_mode = Off
        $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']           = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']           = phpversion();
        $sys_info['ip']             = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';
        $sys_info['max_ex_time']    = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain']         = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit']   = ini_get('memory_limit');
        $mysqlinfo                  = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']  = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            $gd                 = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }
        return $sys_info;
    }
}
